package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.InvoiceService;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.excel.annotation.write.style.ContentRowHeight;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class InvoiceServiceImpl implements InvoiceService {
    @Autowired
    private InvoiceDao invoiceDao;

    @Autowired
    private ShippigOrderDao shippigOrderDao;

    @Autowired
    private PackingDao packingDao;

    @Autowired
    private ExportDao exportDao;

    @Autowired
    private ContractDao contractDao;

    //分页查询
    public PageInfo findAll(int page, int size, InvoiceExample example) {
        PageHelper.startPage(page,size);
        List<Invoice> list = invoiceDao.selectByExample(example);
        return new PageInfo(list);
    }

    //保存
    public void save(Invoice invoice) {
        //i.设置基本数据
        //1.获取id
        String id = invoice.getId();
        //2.根据id查询装箱信息
        Packing packing = packingDao.selectByPrimaryKey(id);
        //3.为invoice设置值
        invoice.setSeller(packing.getSeller());
        invoice.setBuyer(packing.getBuyer());
        invoice.setState(0);
        invoice.setScNo(packing.getExportNos());
        //ii.获取并设置贸易条款
        String  tradeTerms="";
        //1.解析报运单id集合为数组
        String[] exportIds = packing.getExportIds().split(",");
        //2.遍历数组
        for (String exportId : exportIds) {
            //3.根据id查询报运单数据
            Export export = exportDao.selectByPrimaryKey(exportId);
            //4.获取并解析报运单中购销合同id集合
            String[] contractIds = export.getContractIds().split(",");
            //5.遍历购销合同id数组
            for (String contractId : contractIds) {
                //6.根据购销合同id获取购销合同对象
                Contract contract = contractDao.selectByPrimaryKey(contractId);
                //7.获取并拼接贸易条款
                tradeTerms += contract.getTradeTerms()+",";
            }
        }
        //8.设置到invoice
        invoice.setTradeTerms(tradeTerms);
        //9.保存发票信息
        invoiceDao.insert(invoice);
        //设置委托单状态
        ShippigOrder shippigOrder = new ShippigOrder();
        shippigOrder.setId(invoice.getId());
        shippigOrder.setState(1);
        //更新委托单信息
        shippigOrderDao.updateByPrimaryKeySelective(shippigOrder);
    }

    //修改
    public void update(Invoice invoice) {

    }

    //删除
    public void delete(String id) {
        //1.删除发票
        invoiceDao.deleteByPrimaryKey(id);
        //2.创建委托单对象，设置信息
        ShippigOrder shippigOrder = new ShippigOrder();
        shippigOrder.setId(id);
        shippigOrder.setState(0); //设置状态为草稿状态
        //3.修改委托单状态
        shippigOrderDao.updateByPrimaryKeySelective(shippigOrder);

    }

    //根据id查询
    public Invoice findById(String id) {
        return invoiceDao.selectByPrimaryKey(id);
    }

    @Override
    public List<Invoice> findAll(InvoiceExample example) {
        return invoiceDao.selectByExample(example);
    }
}
